//
//  MyRecommendViewController.m
//  HNJYB
//
//  Created by Mag1cPanda on 2017/4/25.
//  Copyright © 2017年 Mag1cPanda. All rights reserved.
//

#import "MyRecommendViewController.h"
#import "MyRecommendCell.h"
#import "MJRefresh.h"

@interface MyRecommendViewController ()
<UITableViewDataSource,
UITableViewDelegate>
{
    UITableView *table;
    NSMutableArray *dataArr;
    NSInteger pagenum;
    NSInteger totalCount;
}

@end

@implementation MyRecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的推荐";
    dataArr = [NSMutableArray array];
    pagenum = 1;
    
    table = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, ScreenWidth-20, ScreenHeight - 64) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.tableFooterView = [UIView new];
    [self.view addSubview:table];
    [table registerNib:[UINib nibWithNibName:@"MyRecommendCell" bundle:nil] forCellReuseIdentifier:@"MyRecommendCell"];
    
//    table.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//       
//        //刷新数据
//        [self refreshData];
//    }];
    
    table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       
        //加载更多
        [self loadMoreData];
    }];
    
    [self loadRecommendDataWithPage:1];
}

-(void)refreshData
{
    pagenum = 1;
    [dataArr removeAllObjects];
    
    NSMutableDictionary *bean = [[NSMutableDictionary alloc] init];
    [bean setValue:[Globle getInstance].userflag forKey:@"userflag"];
    [bean setValue:[Globle getInstance].token forKey:@"token"];
    [bean setValue:@10 forKey:@"pagesize"];
    [bean setValue:@(pagenum) forKey:@"pagenum"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"jjappgetmyrecommend" parameters:bean complete:^(id result, ResultType resultType) {
        
        [hud hideAnimated:YES];
        [table.mj_header endRefreshing];
        
        NSLog(@"jjappgetmyrecommend ~ %@",result);
        
        if ([result[@"restate"] isEqualToString:@"1"]) {
            
            totalCount = [result[@"number"] integerValue];
            
            if ([result[@"data"] isKindOfClass:[NSDictionary class]]) {
                
                NSArray *tmpArr = result[@"data"][@"carlist"];
                if (tmpArr.count > 0) {
                    for (NSDictionary *dic in tmpArr) {
                        if (![dic isEqual:@{}]) {
                            [dataArr addObject:dic];
                        }
                    }
                }
                
                [table reloadData];
                
            }
            
        }
        
    }];
}

-(void)loadMoreData
{
    pagenum++;
    
    [table.mj_footer beginRefreshing];
    
    NSMutableDictionary *bean = [[NSMutableDictionary alloc] init];
    [bean setValue:[Globle getInstance].userflag forKey:@"userflag"];
    [bean setValue:[Globle getInstance].token forKey:@"token"];
    [bean setValue:@10 forKey:@"pagesize"];
    [bean setValue:[NSNumber numberWithInteger:pagenum] forKey:@"pagenum"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"jjappgetmyrecommend" parameters:bean complete:^(id result, ResultType resultType) {
        
        [hud hideAnimated:YES];
        [table.mj_footer endRefreshing];
        
        NSLog(@"jjappgetmyrecommend ~ %@",result);
        
        if ([result[@"restate"] isEqualToString:@"1"]) {
            
            totalCount = [result[@"number"] integerValue];
            
            if ([result[@"data"] isKindOfClass:[NSDictionary class]]) {
                 
                 NSArray *tmpArr = result[@"data"][@"carlist"];
                 if (tmpArr.count > 0) {
                     for (NSDictionary *dic in tmpArr) {
                         if (![dic isEqual:@{}]) {
                             [dataArr addObject:dic];
                         }
                     }
                 }
                 
                 [table reloadData];
                
                if (dataArr.count == totalCount) {
                    [table.mj_footer endRefreshingWithNoMoreData];
                }
             }
            
        }
        
    }];
}

-(void)loadRecommendDataWithPage:(NSInteger)page
{
    NSMutableDictionary *bean = [[NSMutableDictionary alloc] init];
    [bean setValue:[Globle getInstance].userflag forKey:@"userflag"];
    [bean setValue:[Globle getInstance].token forKey:@"token"];
    [bean setValue:@10 forKey:@"pagesize"];
    [bean setValue:[NSNumber numberWithInteger:page] forKey:@"pagenum"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"jjappgetmyrecommend" parameters:bean complete:^(id result, ResultType resultType) {
        
        [hud hideAnimated:YES];
        
        NSLog(@"jjappgetmyrecommend ~ %@",result);
        
        if ([result[@"restate"] isEqualToString:@"1"]) {
            
            totalCount = [result[@"number"] integerValue];
            
            if ([result[@"data"] isKindOfClass:[NSDictionary class]]) {
                NSArray *tmpArr = result[@"data"][@"carlist"];
                if (tmpArr.count > 0) {
                    for (NSDictionary *dic in tmpArr) {
                        if (![dic isEqual:@{}]) {
                            [dataArr addObject:dic];
                        }
                    }
                }
                
                [table reloadData];
                
                if (dataArr.count == totalCount) {
                    [table.mj_footer endRefreshingWithNoMoreData];
                }
            }
            
        }
        
    }];
}

#pragma mark - tableView Delegate & DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyRecommendCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.indexPath = indexPath;
    if (dataArr.count > indexPath.row) {
        cell.dic = dataArr[indexPath.row];
//        cell.indexLab.text = [NSString stringWithFormat:@"%zi.",indexPath.row+1];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (ScreenHeight-64)/10;
}

@end
